using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Common;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.ImageAlbum;
using System.IO;

public partial class admin_webparts_Plug_ins_Images_AlbumInput : System.Web.UI.UserControl
{
    public string _ErrorMessage = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave2.Text = this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.txtDefaultWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultMiniWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultMiniHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtImagePath.InnerHtml = "./" + Constant.UPLOAD_FILE_PATH + "album/&lt;Album ID&gt;/";

        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    if (!"albumedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                    {
                        throw new Exception();
                    }
                    ImageAlbum album = new ImageAlbum(Int32.Parse(Request.QueryString["id"].Trim()));
                    if (album.ID < 1)
                    {
                        throw new Exception();
                    }
                    this.txtAlbumName.Text = album.AlbumName;
                    this.txtDescription.Text = album.Description;
                    this.txtLink.Text = album.LinkURL;
                    this.txtDefaultWidth.Text = album.DefaultWidth.ToString();
                    this.txtDefaultHeight.Text = album.DefaultHeight.ToString();
                    this.txtDefaultMiniWidth.Text = album.MiniDefaultWidth.ToString();
                    this.txtDefaultMiniHeight.Text = album.MiniDefaultHeight.ToString();
                    this.txtImagePath.InnerText = album.ImagePath;
                    this.chkActive.Checked = album.Active;

                    //this.txtAlbumName.Enabled = false;
                }
                catch (Exception)
                {
                    Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"]);
                    return;
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ImageAlbum album = null;

            if ("albumedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                album = new ImageAlbum(Int32.Parse(Request.QueryString["id"].Trim()));
            }
            else
            {
                album = new ImageAlbum();
                album.LanguageID = LanguageUtility.GetLanguageID(Request, Session);
            }

            album.AlbumName = txtAlbumName.Text;
            album.Description = txtDescription.Text;
            album.LinkURL = txtLink.Text;
            album.DefaultWidth = Int32.Parse(txtDefaultWidth.Text.Trim());
            album.DefaultHeight = Int32.Parse(txtDefaultHeight.Text.Trim());
            album.MiniDefaultWidth = Int32.Parse(txtDefaultMiniWidth.Text.Trim());
            album.MiniDefaultHeight = Int32.Parse(txtDefaultMiniHeight.Text.Trim());
            album.Active = chkActive.Checked;

            string uploadedFileName = String.Empty;
            string filePath = "../" + Constant.UPLOAD_FILE_PATH + "album/";
            if (this.fuPresentativeImage.PostedFile != null)
            {
                if (!Directory.Exists(Server.MapPath(filePath)))
                {
                    Directory.CreateDirectory(Server.MapPath(filePath));
                }
                if (this.fuPresentativeImage.PostedFile.FileName.Contains("."))
                {
                    uploadedFileName = DateTime.Now.Ticks.ToString() + this.fuPresentativeImage.PostedFile.FileName.Substring(this.fuPresentativeImage.PostedFile.FileName.LastIndexOf("."));

                    this.fuPresentativeImage.PostedFile.SaveAs(Server.MapPath(filePath + uploadedFileName));
                }
            }

            if ("albumedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                if (uploadedFileName != string.Empty)
                {
                    if (System.IO.File.Exists(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image));
                    }

                    album.Image = "Album_" + album.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + uploadedFileName.Substring(uploadedFileName.LastIndexOf("."));
                    if (System.IO.File.Exists(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image));
                    }
                    System.IO.File.Move(Server.MapPath(filePath + uploadedFileName), Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image));
                }
                album.Update();
            }
            else
            {
                album.ImagePath = Constant.UPLOAD_FILE_PATH + "album/";
                album.Image = "";
                album.Insert();

                if (!System.IO.Directory.Exists(Server.MapPath(filePath + album.ID.ToString() + "/")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(filePath + album.ID.ToString() + "/"));
                }

                if (uploadedFileName != string.Empty)
                {
                    album.Image = "Album_" + album.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + uploadedFileName.Substring(uploadedFileName.LastIndexOf("."));
                    if (System.IO.File.Exists(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image));
                    }
                    System.IO.File.Move(Server.MapPath(filePath + uploadedFileName), Server.MapPath(filePath + album.ID.ToString() + "/" + album.Image));
                }

                album.ImagePath = Constant.UPLOAD_FILE_PATH + "album/" + album.ID.ToString() + "/";
                album.Update();
            }
            
            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"]);
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message;
        }
    }
}
